Completed
Push — master ( 2281e8...8b163e )
by Justin
01:35
created

GedcomX.setDocuments   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
var ExtensibleData = require('./ExtensibleData'),
2
    Person = require('./Person'),
3
    Relationship = require('./Relationship'),
4
    SourceDescription = require('./SourceDescription'),
5
    Agent = require('./Agent'),
6
    Event = require('./Event'),
7
    Document = require('./Document'),
8
    PlaceDescription = require('./PlaceDescription'),
9
    Attribution = require('./Attribution'),
10
    utils = require('./utils');
11
12
/**
13
 * A GEDCOM X document.
14
 * 
15
 * @constructor
16
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
17
 */
18
var GedcomX = function(json){
19
  
20
  // Protect against forgetting the new keyword when calling the constructor
21
  if(!(this instanceof GedcomX)){
22
    return new GedcomX(json);
23
  }
24
  
25
  // If the given object is already an instance then just return it. DON'T copy it.
26
  /*
27
  if(utils.instanceOf(json, this._gedxClass)){
28
    return json;
29
  }
30
  */
31
  
32
  if(GedcomX.isInstance(json)){
33
    return json;
34
  }
35
  
36
  ExtensibleData.call(this, json);
37
  
38
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
39
    this.setPersons(json.persons);
40
    this.setRelationships(json.relationships);
41
    this.setSourceDescriptions(json.sourceDescriptions);
42
    this.setAgents(json.agents);
43
    this.setEvents(json.events);
44
    this.setDocuments(json.documents);
45
    this.setPlaces(json.places);
46
    this.setAttribution(json.attribution);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
47
  }
48
};
49
50
GedcomX.prototype = Object.create(ExtensibleData.prototype);
51
52
GedcomX._gedxClass = GedcomX.prototype._gedxClass = 'GedcomX';
53
54
/**
55
 * Check whether the given object is an instance of this class.
56
 * 
57
 * @param {Object} obj
58
 * @returns {Boolean}
59
 */
60
GedcomX.isInstance = function(obj){
61
  return utils.isInstance(obj, this._gedxClass);
62
};
63
64
/**
65
 * Get the persons
66
 * 
67
 * @returns {Person[]}
68
 */
69
GedcomX.prototype.getPersons = function(){
70
  return this.persons || [];
71
};
72
73
/**
74
 * Set the persons
75
 * 
76
 * @param {Person[]|Object[]} persons
77
 * @returns {GedcomX} This instance
78
 */
79
GedcomX.prototype.setPersons = function(persons){
80
  if(Array.isArray(persons)){
81
    var gedx = this;
82
    gedx.persons = [];
83
    persons.forEach(function(p){
84
      gedx.addPerson(p);
85
    });
86
  }
87
  return this;
88
};
89
90
/**
91
 * Add a person
92
 * 
93
 * @param {Person|Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
94
 * @returns {GedcomX} This instance
95
 */
96
GedcomX.prototype.addPerson = function(person){
97
  if(person){
98
    if(!Array.isArray(this.persons)){
99
      this.persons = [];
100
    }
101
    this.persons.push(Person(person));
102
  }
103
  return this;
104
};
105
106
/**
107
 * Get the relationships
108
 * 
109
 * @returns {Relationship[]}
110
 */
111
GedcomX.prototype.getRelationships = function(){
112
  return this.relationships || [];
113
};
114
115
/**
116
 * Set the relationships
117
 * 
118
 * @param {Relationship[]|Object[]} relationships
119
 * @returns {GedcomX}
120
 */
121
GedcomX.prototype.setRelationships = function(relationships){
122
  if(Array.isArray(relationships)){
123
    var gedx = this;
124
    gedx.relationships = [];
125
    relationships.forEach(function(r){
126
      gedx.addRelationship(r);
127
    });
128
  }
129
  return this;
130
};
131
132
/**
133
 * Add a relationship
134
 * 
135
 * @param {Relationship|Object} relationship
136
 * @returns {GedcomX}
137
 */
138
GedcomX.prototype.addRelationship = function(relationship){
139
  if(relationship){
140
    if(!Array.isArray(this.relationships)){
141
      this.relationships = [];
142
    }
143
    this.relationships.push(Relationship(relationship));
144
  }
145
  return this;
146
};
147
148
/**
149
 * Get the source descriptions
150
 * 
151
 * @returns {SourceDescription[]}
152
 */
153
GedcomX.prototype.getSourceDescriptions = function(){
154
  return this.sourceDescriptions || [];
155
};
156
157
/**
158
 * Set the source descriptions
159
 * 
160
 * @param {SourceDescription[]|Object[]} sourceDescriptions
161
 * @returns {GedcomX}
162
 */
163
GedcomX.prototype.setSourceDescriptions = function(sourceDescriptions){
164
  if(Array.isArray(sourceDescriptions)){
165
    var gedx = this;
166
    gedx.sourceDescriptions = [];
167
    sourceDescriptions.forEach(function(s){
168
      gedx.addSourceDescription(s);
169
    });
170
  }
171
  return this;
172
};
173
174
/**
175
 * Add a ource description
176
 * 
177
 * @param {SourceDescription|Object} sourceDescription
178
 * @returns {GedcomX}
179
 */
180
GedcomX.prototype.addSourceDescription = function(sourceDescription){
181
  if(sourceDescription){
182
    if(!Array.isArray(this.sourceDescriptions)){
183
      this.sourceDescriptions = [];
184
    }
185
    this.sourceDescriptions.push(SourceDescription(sourceDescription));
186
  }
187
  return this;
188
};
189
190
/**
191
 * Get the agents
192
 * 
193
 * @returns {Agent[]}
194
 */
195
GedcomX.prototype.getAgents = function(){
196
  return this.agents || [];
197
};
198
199
/**
200
 * Set the agents
201
 * 
202
 * @param {Agent[]|Object[]} agents
203
 * @returns {GedcomX}
204
 */
205
GedcomX.prototype.setAgents = function(agents){
206
  if(Array.isArray(agents)){
207
    var gedx = this;
208
    gedx.agents = [];
209
    agents.forEach(function(a){
210
      gedx.addAgent(a);
211
    });
212
  }
213
  return this;
214
};
215
216
/**
217
 * Add an agent
218
 * 
219
 * @param {Agent|Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
220
 * @returns {GedcomX}
221
 */
222
GedcomX.prototype.addAgent = function(agent){
223
  if(agent){
224
    if(!Array.isArray(this.agents)){
225
      this.agents = [];
226
    }
227
    this.agents.push(Agent(agent));
228
  }
229
  return this;
230
};
231
232
/**
233
 * Get events
234
 * 
235
 * @returns {Event[]}
236
 */
237
GedcomX.prototype.getEvents = function(){
238
  return this.events || [];
239
};
240
241
/**
242
 * Set events
243
 * 
244
 * @param {Event[]|Object[]} events
245
 * @returns {GedcomX}
246
 */
247
GedcomX.prototype.setEvents = function(events){
248
  if(Array.isArray(events)){
249
    var gedx = this;
250
    gedx.events = [];
251
    events.forEach(function(event){
252
      gedx.addEvent(event);
253
    });
254
  }
255
  return this;
256
};
257
258
/**
259
 * Add an event
260
 * 
261
 * @param {Event|Object} event
262
 * @returns {GedcomX}
263
 */
264
GedcomX.prototype.addEvent = function(event){
265
  if(event){
266
    if(!Array.isArray(this.events)){
267
      this.events = [];
268
    }
269
    this.events.push(Event(event));
270
  }
271
  return this;
272
};
273
274
/**
275
 * Get the documents
276
 * 
277
 * @returns {Document[]}
278
 */
279
GedcomX.prototype.getDocuments = function(){
280
  return this.documents || [];
281
};
282
283
/**
284
 * Set the documents
285
 * 
286
 * @param {Documents[]|Object[]} documents
287
 * @returns {GedcomX}
288
 */
289
GedcomX.prototype.setDocuments = function(documents){
290
  if(Array.isArray(documents)){
291
    var gedx = this;
292
    gedx.documents = [];
293
    documents.forEach(function(doc){
294
      gedx.addDocument(doc);
295
    });
296
  }
297
  return this;
298
};
299
300
/**
301
 * Add a document
302
 * 
303
 * @param {Document|Object} doc
304
 * @returns {GedcomX}
305
 */
306
GedcomX.prototype.addDocument = function(doc){
307
  if(doc){
308
    if(!Array.isArray(this.documents)){
309
      this.documents = [];
310
    }
311
    this.documents.push(Document(doc));
312
  }
313
  return this;
314
};
315
316
/**
317
 * Get places
318
 * 
319
 * @returns {PlaceDescription[]}
320
 */
321
GedcomX.prototype.getPlaces = function(){
322
  return this.places || [];
323
};
324
325
/**
326
 * Set the places
327
 * 
328
 * @param {PlaceDescription[]|Object} places
329
 * @returns {GedcomX}
330
 */
331
GedcomX.prototype.setPlaces = function(places){
332
  if(Array.isArray(places)){
333
    var gedx = this;
334
    gedx.places = [];
335
    places.forEach(function(place){
336
      gedx.addPlace(place);
337
    });
338
  }
339
  return this;
340
};
341
342
/**
343
 * Add a place
344
 * 
345
 * @param {PlaceDescription} place
346
 * @returns {GedcomX}
347
 */
348
GedcomX.prototype.addPlace = function(place){
349
  if(place){
350
    if(!Array.isArray(this.places)){
351
      this.places = [];
352
    }
353
    this.places.push(PlaceDescription(place));
354
  }
355
  return this;
356
};
357
358
/**
359
 * Get attritbution
360
 * 
361
 * @returns {Attribution}
362
 */
363
GedcomX.prototype.getAttribution = function(){
364
  return this.attribution;
365
};
366
367
/**
368
 * Set attribution
369
 * 
370
 * @param {Attribution} attribution
371
 * @returns {GedcomX}
372
 */
373
GedcomX.prototype.setAttribution = function(attribution){
374
  if(attribution){
375
    this.attribution = Attribution(attribution);
376
  }
377
  return this;
378
};
379
380
/**
381
 * Export the object as JSON
382
 * 
383
 * @return {Object} JSON object
384
 */
385
GedcomX.prototype.toJSON = function(){
386
  var json = ExtensibleData.prototype.toJSON.call(this);
387
  
388
  if(this.persons){
389
    json.persons = this.persons.map(function(p){
390
      return p.toJSON();
391
    });
392
  }
393
  
394
  if(this.relationships){
395
    json.relationships = this.relationships.map(function(r){
396
      return r.toJSON();
397
    });
398
  }
399
  
400
  if(this.sourceDescriptions){
401
    json.sourceDescriptions = this.sourceDescriptions.map(function(s){
402
      return s.toJSON();
403
    });
404
  }
405
  
406
  if(this.agents){
407
    json.agents = this.agents.map(function(a){
408
      return a.toJSON();
409
    });
410
  }
411
  
412
  if(this.events){
413
    json.events = this.events.map(function(e){
414
      return e.toJSON();
415
    });
416
  }
417
  
418
  if(this.documents){
419
    json.documents = this.documents.map(function(d){
420
      return d.toJSON();
421
    });
422
  }
423
  
424
  if(this.places){
425
    json.places = this.places.map(function(p){
426
      return p.toJSON();
427
    });
428
  }
429
  
430
  if(this.attribution){
431
    json.attribution = this.attribution.toJSON();
432
  }
433
  
434
  return json;
435
};
436
437
// Expose all classes
438
GedcomX.Address = require('./Address');
439
GedcomX.Agent = Agent;
440
GedcomX.Attribution = Attribution;
441
GedcomX.Conclusion = require('./Conclusion');
442
GedcomX.Coverage = require('./Coverage');
443
GedcomX.Date = require('./Date');
444
GedcomX.Document = Document;
445
GedcomX.Event = Event;
446
GedcomX.EventRole = require('./EventRole');
447
GedcomX.EvidenceReference = require('./EvidenceReference');
448
GedcomX.ExtensibleData = ExtensibleData;
449
GedcomX.Fact = require('./Fact');
450
GedcomX.Gender = require('./Gender');
451
GedcomX.Identifiers = require('./Identifiers');
452
GedcomX.Name = require('./Name');
453
GedcomX.NameForm = require('./NameForm');
454
GedcomX.NamePart = require('./NamePart');
455
GedcomX.Note = require('./Note');
456
GedcomX.OnlineAccount = require('./OnlineAccount');
457
GedcomX.Person = Person;
458
GedcomX.PlaceDescription = PlaceDescription;
459
GedcomX.PlaceReference = require('./PlaceReference');
460
GedcomX.Qualifier = require('./Qualifier');
461
GedcomX.Relationship = Relationship;
462
GedcomX.ResourceReference = require('./ResourceReference');
463
GedcomX.SourceCitation = require('./SourceCitation');
464
GedcomX.SourceDescription = SourceDescription;
465
GedcomX.SourceReference = require('./SourceReference');
466
GedcomX.Subject = require('./Subject');
467
GedcomX.TextValue = require('./TextValue');
468
469
module.exports = GedcomX;